/**
 * product_create 页面的 lecture 分页
 */
$(function () {
	_label();
})

//弹出表格层
function add_lecture(){
	var val = $("input[name='keywords']").val();//参数
	var ids = $("#lid").val();
    $.ajax({
    	url: "../lecture/getLectureList.do",
    	datatype: 'json',
    	type: "post",
    	data: {
    		param:val,
    		ids:ids
    	},
    	success: function (data) {
    		if (data != null) {
    			var html = "";
    			for(var i in data){
    				html = html + "<tr>";
       				html = html + "<td class='center'><input name='aaa' type='checkbox' value='1'></input></td>";
       				html = html + "<td class='center'>"+data[i].lid+"</td>";
       				html = html + "<td class='center'>"+data[i].name+"</td>";
       				html = html + "<td class='center'><span class='label arrowed label-primary'>";
       				html = html + "<input type='button' id='state' value='直播' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' />";
       				html = html + "</span></td>";
       				html = html + "<td class='center'>"+data[i].datetime+"</td>";
       				html = html + "<td class='center'>"+data[i].daka+"</td>";	
       				html = html + "</tr>";
    			}
       			$("#list").html(html);
       			document.getElementById('light').style.display='block';
    		}
    	}
    });
}

function _label(){
	$.ajax({
		url:'../dictionary/lectureLabel.do',
		dataType:'json',
		type: "post",
		success:function(data){
			var html="";
			for(var i=0;i<data.data.length;i++){
				html += "<label><input id=\"2001\" name=\"label\" type=\"checkbox\" value=\""+data.data[i]+"\" onClick=\"check_count('xingge', this, 3)\">"+data.data[i]+"</label>";
			}
			$('#edit_xingge').html(html); 
		}
		
	});
}

//用于产品添加页面产品类型控制
function change_producttype(type){
	var timeproduct=document.getElementById("timeproduct");
	var countproduct=document.getElementById("countproduct");
	var packproduct=document.getElementById("packproduct");
	if(type=="计时产品"){
		timeproduct.style.display="block";
		countproduct.style.display="none";
		packproduct.style.display="none";
	}
	if(type=="计次产品"){
		timeproduct.style.display="none";
		countproduct.style.display="block";
		packproduct.style.display="none";
	}
	if(type=="打包产品"){
		timeproduct.style.display="none";
		countproduct.style.display="none";
		packproduct.style.display="block";
	}
}

//产品添加页面标签控制
function check_count(id, my, num) {
	var label = $("#label_id");
	var temp=" ";
	var oEvent = document.getElementById('edit_' + id);
	var chks = oEvent.getElementsByTagName("INPUT");
	var count = 0;
	for (var i = 0; i < chks.length; i++) {
		if (chks[i].type == "checkbox") {
			if (chks[i].checked == true) {
				temp+=chks[i].value+",";
				count++;
			}
			if (count > num) {
				my.checked = false;
				alert('最多只能选择' + num + '项！');
				return false;
			}
		}
	}
	label.val(temp);
//	alert(label.val());
}

//验证当前页是否已经存在选中的行
function _isclickinpage(id){
	var lid = $("#lid").val();
	var ids = lid.split(",");
	var is;
	//判断当前选择的已经被选中
	if(lid.indexOf(id)>-1){
		is = 0;
	}else{
		is = 1;
	}
	return is;
}

//单选
/*function _clickid(_this){
	var id = _this.parent().parent().children('td')[1].childNodes[0].textContent;//获取当前行的ID值
	var tempid = $("#tempid");
	var oldid = tempid.val();//获取隐藏域已存在的值
	var temp="";
	if(_this.is(':checked')){//判断当前行是否被选中
		if (oldid == null || oldid == "") {
			temp = id;
		}else{
			temp = oldid + "," + id;
		}
	}else{
		if (oldid != null || oldid != "") {
			var is1 = ",".indexOf(oldid.split(id)[0]);// 获取当前字符前的字符
			var is2 = ",".indexOf(oldid.split(id)[1]);// 获取当前字符后的字符
			if (is1 != -1 && is2 == -1) {
				temp = oldid.replace(id + ",", "");
			} else if (is1 == -1 && is2 == -1) {
				temp = oldid.replace(id + ",", "");
			} else if (is1 == -1 && is2 != -1) {
				temp = oldid.replace("," + id, "");
			}
		}
		_this.parent().parent().removeAttr("style");//移除标红样式
	}
	tempid.val(temp);
	console.log(tempid.val()+"单选");
}*/

//已添加的讲座列表全选
function selectAll1(){
	var a = $("#is input[type=checkbox]");//获取表格里所有的复选框
	var b = $("#is input[type=checkbox]:checked");//获取选中的复选框
	console.log(a);
	console.log(b);
	if(b.length>0&&b.length<a.length){
		b.each(function(){
			$(this).prop("checked", false);//移除选中
//			_clickid($(this));
		});
	}else if(b.length==a.length){
		b.each(function(){
			$(this).prop("checked", false);
//			_clickid($(this));
		});
		return;//强制中断
	}
	a.each(function(){//遍历复选框
		if($(this).is(':checked')){
			$(this).prop("checked", false);
//			_clickid($(this));
		}else{
			$(this).prop("checked", true);
//			_clickid($(this));
		}
	});
}

//频道下所有讲座全选
function selectAll(){
	var a = $("#nois input[type=checkbox]");//获取表格里所有的复选框
	var b = $("#nois input[type=checkbox]:checked");//获取选中的复选框
	console.log(a);
	console.log(b);
	if(b.length>0&&b.length<a.length){
		b.each(function(){
			$(this).prop("checked", false);//移除选中
//			_clickid($(this));
		});
	}else if(b.length==a.length){
		b.each(function(){
			$(this).prop("checked", false);
//			_clickid($(this));
		});
		return;//强制中断
	}
	a.each(function(){//遍历复选框
		if($(this).is(':checked')){
			$(this).prop("checked", false);
//			_clickid($(this));
		}else{
			$(this).prop("checked", true);
//			_clickid($(this));
		}
	});
}

//绑定课程
function product_btn_add() {

	var a = $("input:checkbox[name='aaa']").parent().parent();
	a.css({
		color : "#393939"
	})
	var length = $("input:checkbox[name='aaa']:checked").length;
	if (length) {
		var b = $("input:checkbox[name='aaa']:checked").parent().parent();
		for (var i = 0; i < length; i++) {
			b.children().children('input')[i].name = "aaa1";
		}
		var tbody = document.getElementById("lecture_add").getElementsByTagName("tbody");
		$(tbody).append(b);
		
		//往隐藏域写入选中的数据
		var lid = $("#lid").val();
		var input = $("#is input[type=checkbox]");//获取选中的复选框
		input.each(function(){
			var id = $(this).parent().parent().children('td')[1].childNodes[0].textContent;
			if (lid == null || lid == "") {
				lid += id;
			}else{
				lid += "," + id;
			}
		})
		$("#lid").val(lid);
//		alert(temp);
		add_lecture();
	} else {
		alert("请选择至少一个讲座添加到该产品！");
	}
}

//删除已经绑定的课程
function product_btn_delete(){
	
	// console.log($("input:checkbox[name='aaa']:checked").length);
	var a=$("input:checkbox[name='aaa1']:checked").parent().parent().children();
	var b = $("#is input[type=checkbox]:checked");//获取选中的复选框
	var lid = $("#lid").val();
	if(b.length==0){
		alert("请选择要移除的讲座！")
	}else{
		b.each(function(){
//			alert(lid+"--之前--");
			var id = $(this).parent().parent().children('td')[1].childNodes[0].textContent;
//			alert(id+"--之中--");
			if (lid != null || lid != "") {
				var is1 = ",".indexOf(lid.split(id)[0]);// 获取当前字符前的字符
				var is2 = ",".indexOf(lid.split(id)[1]);// 获取当前字符后的字符
				if (is1 != -1 && is2 == -1) {
					lid = lid.replace(id + ",", "");
				}
				if (is1 == -1 && is2 == -1) {
					lid = lid.replace(id + ",", "");
				}
				if (is1 == -1 && is2 != -1) {
					lid = lid.replace("," + id, "");
				}
				if(is1 != -1 && is2 != -1){
					lid = lid.replace(id, "");
				}
			}
//			lid = lid.replace(id + ",", "");
		});
//		alert(lid+"--之后--");
		a.remove();
		$("#lid").val(lid);
		add_lecture();
		console.log($("#lid").val()+"移除")
	} 
}

//隐藏弹窗
function return_lecture(){
	document.getElementById('light').style.display='none';
}

//创建产品页面验证
function CheckPorduct(form){
	var img0=document.getElementsByName("img0").item(0).src.indexOf('product_create.html');
	var img1=document.getElementsByName("img0").item(0).src.indexOf('product_edit.html');
	var lecture_label=$("input:checkbox[name='label']:checked").length;
	// var type=$("input:radio[name='lecture_type']:checked").val();
	var product_attributes=$("option:selected").val();
	var lecture_add=document.getElementById("lecture_add");
	var iftr=$($(lecture_add).children('tbody')[0]).children('tr')[0];

	if(img0>0){
			alert("请选择频道封面");
			form.img0.focus();
			return false;
	}
	if(img1>0){
			alert("请选择频道封面");
			form.img0.focus();
			return false;
	}
	else if(form.name.value==""){
			alert("请输入产品名称！");
			form.name.focus();
		return false;		
	}
	else if(lecture_label==0){
			alert("请至少选择一个产品标签！");
		return false;		
	}
	else if(form.text.value==""){
			alert("请填写产品描述！");
			form.text.focus();
		return false;		
	}
	else if(product_attributes=="计次产品"&&form.istime2.value==""){
				alert("请输入总收听次数！");
				form.istime2.focus();
		return false;
	}
	else if(iftr==undefined){
			alert("请点击“添加讲座到该产品”至少选择一个讲座！");
			form.add_lecture .focus();
		return false;
	}
	else if(form.price.value==""){
				alert("请输入产品价格！");
				form.price.focus();
		return false;
	}
	if(confirm("产品一旦经平台审核通过发布后产生订单，产品的讲座列表将只能进行新增讲座操作，无法删除既有讲座。\u000d请您确认是否现在进行产品创创建提交审核？")){
		AjaxSubmit();
	    return true;
	}else{
	    return false;
	}
}

//提交表单
function AjaxSubmit(){
	$("#product_form").submit(function () {   
        $("#product_form").ajaxSubmit({
            type: "post",
            url: "../product/addProduct.do",
            success: function (data) {
            	if(data.success){
            		window.location.href="../redirect/product_productlist.do";
            	}else{
            		alert(data.msg);
            	}
            }
       });
        return false;
    });
    $("#product_form").submit();
}